<!DOCTYPE html>
<html lang="">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>

<body>

</body>
<script>
    var api_url = "https://trade.sinvocloud.com/api/";
    var main_url = "https://trade.sinvocloud.com/mobile/";

    function fun() {
        var code = getUrlParam('code');
        var state = getUrlParam('state');
        localStorage.setItem('state', state);
        if (code) {
            $.ajax({
                type: 'get',
                url: api_url + 'wechat/callback',
                data: { code: code, state: state },
                success: function (res) {
                    if (res.code === 200) {
                        localStorage.setItem("token", res.data.token);
                        localStorage.setItem('nowTime', new Date().getTime())
                        window.location.href = main_url + '#/phone';
                    } else if (res.data.appid) {
                        window.location.href = `${main_url}#/weixinAuth?market_id=${state}`
                    }
                },
                error: function (jqXHR) {
                    console.log("Error: " + JSON.stringify(jqXHR));
                }
            })
        } else {
            window.location.href = `${main_url}/#/weixinAuth?market_id=${state}`
        }


    }
    fun()
    function getUrlParam(name) {
        //构造一个含有目标参数的正则表达式对象

        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");

        //匹配目标参数

        var r = window.location.search.substr(1).match(reg);

        //返回参数值

        if (r != null) return unescape(r[2]);

        return null;

    }



</script>

</script>

</html>